﻿using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows;

using iStudio.Structure.Engine;
using iStudio.Structure.Wave;

namespace iStudio.Module.Equalizer
{
    /// <summary>
    /// Interaction logic for EqualizerWindow.xaml
    /// </summary>
    public partial class EqualizerWindow
    {
        private readonly EqualizerEffect _eqCore;
        private int _indexBefore;

        public EqualizerWindow(Structure.Engine.Module module): base(module)
        {
            InitializeComponent();

            Title = "Equalizer";

            _eqCore = new EqualizerEffect();
            
            main_equalizer.SliderChangedValue += EqualizerChangedValue;
            main_equalizer.FrequencyValues = (new[] { 32f, 125f, 500f, 1000f, 2000f, 4000f, 8000f, 16000f });

            output.SelectedIndex = 0;
            _indexBefore = 0;

            SetEqualizer(0);
        }

        public override bool Close()
        {
            UnsetEqualizer(_indexBefore);

            return base.Close();
        }

        private void EqualizerChangedValue (object sender, Structure.Control.Audio.Equalizer.SliderChangedEventArgs args)
        {
            _eqCore.Values[args.Index] = (float)args.Value;
        }

        private void SetOutputButtonClick(object sender, RoutedEventArgs e)
        {
            UnsetEqualizer(_indexBefore);

            SetEqualizer(output.SelectedIndex);

            //List<float> buffer = new List<float>();

            //foreach (WaveTrack track in AudioEngine.Instance.Project.TrackList)
            //{
            //    for (int i = 0; i < track.Length; i++)
            //    {
            //        buffer.Add (track.GetSample (i));
            //    }
            //}

            //float[] byBuffer = buffer.ToArray ();

            //_eqCore.Process (byBuffer, 0, byBuffer.Length);

            //StringBuilder build = new StringBuilder();

            //foreach (WaveTrack track in AudioEngine.Instance.Project.TrackList)
            //{
            //    for (int i = 0; i < track.Length; i++)
            //    {
            //        build.AppendLine (byBuffer[i].ToString (CultureInfo.InvariantCulture));
            //        //track.SetSample (i, byBuffer[i]);
            //    }
            //}

            //File.WriteAllText ("test.txt", build.ToString());
        }

        private void SetEqualizer(int indexTo)
        {
            if (indexTo == 0)
            {
                AudioEngine.Instance.GetEngineOutputProviders().Providers.Add(_eqCore);
            }
            else
            {
                AudioEngine.Instance.GetChannelOutputProviders((WaveChannel)(indexTo - 1)).Providers.Add(_eqCore);
            }

            _indexBefore = indexTo;
        }

        private void UnsetEqualizer(int indexFrom)
        {
            OutputSampleProvider provider = indexFrom == 0 ? AudioEngine.Instance.GetEngineOutputProviders() : AudioEngine.Instance.GetChannelOutputProviders((WaveChannel)(indexFrom - 1));

            int foundIndex = -1;

            for (int i = 0; i < provider.Providers.Count; i++)
            {
                if (provider.Providers[i] == _eqCore)
                {
                    foundIndex = i;
                }
            }

            if (foundIndex >= 0)
            {
                provider.Providers.RemoveAt(foundIndex);
            }
        }
    }
}
